package stack2;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-11-02
 * Time:15:55
 */
public class MyStack {
    public int[] elem;
    public int usedSize;
    public  MyStack(){
        this.elem = new int[5];
    }
    public void push(int val){
        if(isFull()){
            System.out.println("栈满进行扩容");
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize++] = val;
    }
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空");
        }
        int ret = this.elem[usedSize - 1];
        this.usedSize--;
        return ret;
    }
    public boolean isFull(){
        return this.usedSize == this.elem.length;
    }
    public  boolean isEmpty(){
        return this.usedSize == 0;
    }
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("栈空");
        }
        return this.elem[usedSize - 1];
    }
}
